display.setStatusBar(display.HiddenStatusBar)

local lime = require("lime")
lime.disableScreenCulling()

require("physics")
physics.start()
local ui = require("ui")
local remote = require("remote")
remote.startServer("8080")


local map = lime.loadMap("level1.tmx")




local onPlayerProperty = function(property, type, object)
    player = object.sprite
end
map:addPropertyListener("IsPlayer", onPlayerProperty)


local visual = lime.createVisual(map)

local physical = lime.buildPhysical(map)
map:setFocus(player)
local acc={}




--local onTouch = function(event)
--    if player.canJump then
--        player:applyLinearImpulse( 0, -10, player.x, player.y )
--    end
--end

--Runtime:addEventListener( "touch", onTouch )

--local physicsData = (require "level1").physicsData()
--physics.addBody(map.world, "static", physicsData:get("level1"))

local STATE_IDLE = "Idle"
local STATE_WALKING = "Walking"
local DIRECTION_LEFT = -1
local DIRECTION_RIGHT = 1
local STATE_JUMPING = "Jumping"

player.state = STATE_JUMPNG


local onButtonLeftEvent = function(event)
    if event.phase == "press" then
               player.direction = DIRECTION_LEFT
               player.state = STATE_WALKING
               player.xScale = player.direction
           else
               player.state=STATE_IDLE
           end  
            player:prepare("anim" .. player.state)
            player:play()
end

local onButtonRightEvent = function(event)
       if event.phase == "press" then
               player.direction = DIRECTION_RIGHT
               player.state = STATE_WALKING
               player.xScale = player.direction
           else
               player.state=STATE_IDLE
           end 
           player:prepare("anim" .. player.state)
            player:play()
      end
  
  function onTiltRight(event)
      
      print (event.yGratity)
      if(event.yGravity<0) then
          player.direction = DIRECTION_LEFT
               player.state = STATE_WALKING
               player.xScale = player.direction
           elseif(event.yGravity > 0) then
               player.direction = DIRECTION_RIGHT
               player.state = STATE_WALKING
               player.xScale = player.direction
            end
          
   end
  
  
local onButtonAPress = function(event)
 
    if player.canJump then
        player.state = STATE_JUMPING
       
        player:applyLinearImpulse(0, -5, player.x, player.y)
    end
     player:prepare("anim" .. player.state)
     player:play()
end

local onUpdate = function(event)
    
    local yGravity = remote.yGravity
    print(yGravity)
    if(yGravity ~= nil) then
        
      if(yGravity<-0.5) then
          player.direction = DIRECTION_LEFT
               player.state = STATE_WALKING
               player.xScale = player.direction
           elseif(yGravity > 0.5) then
               player.direction = DIRECTION_RIGHT
               player.state = STATE_WALKING
               player.xScale = player.direction
           end
           
           
         end
        
  
    
    map:update( event )
    if player.state == STATE_WALKING then
        player:applyForce(player.direction *3, 0,player.x, player.y)
        
    elseif player.state == STATE_IDLE then
        local vx, vy = player:getLinearVelocity()
        --print(vx)
        
        if vx ~=0 then
            player:setLinearVelocity(vx*0.9, vy)
        end
    end
end

Runtime:addEventListener("enterFrame", onUpdate)

Runtime:addEventListener("accelerometer", onTiltRight)
        
        



local buttonLeft = ui.newButton{
    default = "sprites/buttonLeft.png",
    over = "sprites/buttonLeft_over.png",
    onEvent = onButtonLeftEvent
}
buttonLeft.x = buttonLeft.width / 2 + 10
buttonLeft.y = display.contentHeight - buttonLeft.height / 2 - 10
local buttonRight = ui.newButton{
    default = "sprites/buttonRight.png",
    over = "sprites/buttonRight_over.png",
    onEvent = onButtonRightEvent
}
buttonRight.x = buttonLeft.x + buttonRight.width
buttonRight.y = buttonLeft.y
local buttonA = ui.newButton{
    default = "sprites/buttonA.png",
    over = "sprites/buttonA_over.png",
    onEvent = onButtonAPress
}
buttonA.x = display.contentWidth - buttonA.width / 2
buttonA.y = display.contentHeight - buttonA.height / 2 - 10







local function onCollision( self, event )

 	if ( event.phase == "began" ) then
            
		if event.other.isGround then
			player.canJump = true
                        
                 if player.state == STATE_JUMPING then
				player.state = STATE_IDLE
			
				player:prepare("anim" .. player.state)
				player:play()
			end       
                        
	end
            elseif ( event.phase == "ended" ) then
                print (event.other.isGround)
		if event.other.isGround then
			player.canJump = false
                        print (player.canJump)
		end
	end
end










player.collision = onCollision
player:addEventListener( "collision", player )

physics.setDrawMode("dynamic")
player.state = STATE_IDLE
player:prepare("anim" .. player.state)
player:play()